import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/model/commodity_cart.dart';
import 'package:flutter_shop_z/pages/cart_page/bloc/cart_bloc.dart';
import 'package:flutter_shop_z/pages/cart_page/cart_bottom.dart';
import 'package:flutter_shop_z/pages/cart_page/cart_item.dart';
import 'package:flutter_shop_z/shared_preferences/commodity_cart_prefs.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';

class CartPage extends StatefulWidget {
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  @override
  void initState() {
    updateCartBloc();
    super.initState();
  }

  double allPrice = 0; //总价格
  int allGoodsCount = 0; //商品总数量
  bool isAllCheck = true; //全选

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('购物车'),
      ),
      body: BlocBuilder<CartBloc, CartState>(builder: (context, state) {
        if (state.listCartStr == null || state.listCartStr.length <= 0) {
          return Center(
            child: Text('快去添加你喜欢的商品吧'),
          );
        } else {
          List<CommodityCart> listCart = decodeList(state.listCartStr);
          allPrice = 0; //总价格
          allGoodsCount = 0; //商品总数量
          isAllCheck = true;
          listCart.forEach((element) {
            if (element.isCheck) {
              allGoodsCount += element.number;
              allPrice += (element.number *
                      (double.parse(element.commodityDiscountPrice) * 100)) /
                  100;
            } else {
              isAllCheck = false;
            }
          });
          if (listCart == null || listCart.length == 0) {
            return Center(
              child: Text('快去添加你喜欢的商品吧'),
            );
          } else {
            updateCartBloc();
            return Stack(
              children: <Widget>[
                ListView.builder(
                  itemBuilder: (context, index) {
                    return CartItem(listCart[index], listCart, index);
                  },
                  itemCount: listCart.length,
                  padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(120)),
                ),
                Positioned(
                  child: CartBottom(allGoodsCount, allPrice, isAllCheck),
                  bottom: 0,
                  left: 0,
                ),
              ],
            );
          }
        }
      }),
    );
  }

  List<CommodityCart> decodeList(String json) {
    List<CommodityCart> commodityList = [];
    if (json != null) {
      List list = jsonDecode(json);
      if (list.length > 0) {
        list.forEach((element) {
          if (element != null) {
            CommodityCart cart = CommodityCart.fromJson(element);
            commodityList.add(cart);
          }
        });
      }
    } else {
      //告诉界面为空
      commodityList = [];
    }
    return commodityList;
  }

  void updateCartBloc() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    BlocProvider.of<CartBloc>(context).add(SetCartEvent(
        sharedPreferences.getString(CommodityCartPrefs.cartListKey)));
  }
}
